/********************************************************************
	created:	2012/02/26   23:20
	filename: 	hy_mutex.h
	author:		supercaco@sina.com
	purpose:	
*********************************************************************/
#ifndef _HY_MUTEX_H_
#define _HY_MUTEX_H_


	class hy_fast_mutex //: private zr_noncopyable
	{
	public:

		// Constructor.
		hy_fast_mutex(bool lock_flag = true):_lock_flag(lock_flag)
		{
			if (lock_flag)
			{
#ifdef _WIN32
				::InitializeCriticalSection(&mutex_);
#else
				::pthread_mutex_init(&mutex_, 0);
#endif
			}

		}

		// Destructor.
		~hy_fast_mutex()
		{
			if (_lock_flag)
			{
#ifdef _WIN32
				::DeleteCriticalSection(&mutex_);
#else
				::pthread_mutex_destroy(&mutex_);
#endif	
			}

		}

		// Lock the mutex.
		void lock()
		{
			if (_lock_flag)
			{
#ifdef _WIN32
				::EnterCriticalSection(&mutex_);
#else
				::pthread_mutex_lock(&mutex_);
#endif	
			}
		}

		// Unlock the mutex.
		void unlock()
		{
			if (_lock_flag)
			{
#ifdef _WIN32
				::LeaveCriticalSection(&mutex_);
#else
				::pthread_mutex_unlock(&mutex_);
#endif	
			}
		}

	private:
		bool _lock_flag;
#ifdef _WIN32
		::CRITICAL_SECTION mutex_;
#else
		::pthread_mutex_t mutex_;
#endif
	};

	typedef	hy_fast_mutex	hy_mutex;

#endif  //_HY_MUTEX_H_
